package server.phone.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;
import server.phone.dao.BillDAO;
import server.phone.dao.CashierDAO;

import com.opensymphony.xwork2.ActionSupport;
import com.utopia84.model.Bill;
import com.utopia84.model.Billandpdt;
import com.utopia84.model.Cashier;
import com.utopia84.model.Cashierrecord;

public class SetCashierRecordServlet extends ActionSupport {
	private static final long serialVersionUID = -7811568044252827351L;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public SetCashierRecordServlet(){
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
	}
	public void doGet()	throws  IOException {
							
		String waiter = request.getParameter("waiter");
		String time = request.getParameter("time");
		float money = Float.parseFloat(request.getParameter("money"));
		String description = request.getParameter("description");				
		String cashierId = request.getParameter("cashierId");
		
		int cId = 0;
		
		try {
			CashierDAO cd = new CashierDAO();
			cId = cd.findCIdByCashierId(cashierId);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("SetCashierRecord:cId="+cId+";cashierId="+cashierId+";waiter="+waiter+
				";time="+time+";money="+money+";description="+description);
		List<Cashierrecord> cash = new ArrayList<Cashierrecord>();
		
		Cashierrecord c = new Cashierrecord();
		c.setWaiter(waiter);
		c.setTime(time);
		c.setMoney(money);
		c.setDescription(description);
		c.setCId(cId);
		
		cash.add(c);
		int flag = 0;
		try {
			CashierDAO cd = new CashierDAO();
			flag = cd.saveCashierRecord(cash);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		JSONObject jObject = new JSONObject();
		try {
			if (flag==1)
				jObject.put("ret", "success");
		} catch (Exception e) {
			jObject.put("ret", "error");
		}

		PrintWriter out = response.getWriter();
		out.println(jObject);
		out.flush();
		out.close();

	}

}
